package com.client.service;

import org.apache.log4j.Logger;

import com.client.model.IPayData;
import com.client.model.IPayOrder;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class IPayOrderServiceImpl extends AbstractService implements IPayOrderService {
	
	private Logger logger;
	
	public IPayOrderServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	@Override
	public boolean savePayOrder(IPayOrder po) {
		po.setId(getTableId("ipayorder"));
		String sql = "insert into ipayorder(id,orderid,suborderid,userid" +
				",mobileno,carrier,ordertype,codetype," +
				"spid,spcodeid,province,region,feevalue," +
				"channelid,projectid,report,ordertime" +
				",reporttime" +
				",cpid,appid,goodscode,goodsname,chid,requestfee,successfee,imsi) " +
				" values ("+po.getId()+",'"+po.getOrderId()+"','"+
				po.getSuborderId()+"','"+po.getUserId()+"','"+po.getMobileNo()+
				"','"+po.getCarrier()+"','"+po.getOrderType()+"','"+po.getCodeType()+
				"','"+po.getSpId()+"','"+po.getSpCodeId()+"','"+po.getProvince()+
				"','"+po.getRegion()+"','"+po.getFeeValue()+"','"+po.getChannelId()+
				"','"+po.getProjectId()+"','"+po.getReport()+"','"+com.util.Util.dateToString(po.getOrderTime(), "yyyy-MM-dd HH:mm:ss") +
				"','"+com.util.Util.dateToString(po.getReportTime(), "yyyy-MM-dd HH:mm:ss")+"'" +
				",'"+po.getCpid()+"','"+po.getAppid()+"','"+po.getGoodscode()+"'" +
				",'"+po.getGoodsname()+"','"+po.getChid()+"','"+po.getRequestfee()+"','"+po.getSuccessfee()+"'" +
				",'"+po.getImsi()+"')";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}

	@Override
	public boolean updatePayOrder(IPayOrder po) {
		String sql = "update ipayorder set report='"+po.getReport()+"',reporttime='"
		+com.util.Util.dateToString(po.getReportTime(), "yyyy-MM-dd HH:mm:ss")+"'" +
				",successfee='"+po.getSuccessfee()+"' where orderid='"+po.getOrderId()+"'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public boolean savePayData(IPayData iPayData) {
		iPayData.setId(getTableId("ipaydata"));
		String sql ="insert into ipaydata(" +
				"id,day,hour,minute,channel" +
				",project,carrier,ordertype," +
				"codetype,spid,spcodeid,province," +
				"region,count,resultcount,succ,feevalue,datakey,issuedcount" +
				",cpid,appid,goodscode,goodsname,chid,requestfee,successfee) " +
				" values ('"+iPayData.getId()+
				"','"+iPayData.getDay()+"','"+iPayData.getHour()+
				"','"+iPayData.getMinute()+"','"+iPayData.getChannel()+
				"','"+iPayData.getProject()+"','"+iPayData.getCarrier()+
				"','"+iPayData.getOrderType()+"','"+iPayData.getCodeType()+
				"','"+iPayData.getSpId()+"','"+iPayData.getSpCodeId()+
				"','"+iPayData.getProvince()+"','"+iPayData.getRegion()+
				"','"+iPayData.getCount()+"','"+iPayData.getResultcount()+"','"+iPayData.getSucc()+
				"','"+String.valueOf((int)iPayData.getFeevalue())+"','"+iPayData.getDataKey()+"','"+iPayData.getIssuedcount()+"'" +
				",'"+iPayData.getCpid()+"','"+iPayData.getAppid()+"','"+iPayData.getGoodscode()+"'" +
				",'"+iPayData.getGoodsname()+"','"+iPayData.getChid()+"'" +
				",'"+String.valueOf((int)iPayData.getRequestfee())+"','"+String.valueOf((int)iPayData.getSuccessfee())+"')";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}

	@Override
	public boolean updatePayData(IPayData iPayData) {
		String sql = "update ipaydata set count='"+iPayData.getCount()+"',issuedcount='"+iPayData.getIssuedcount()+"'," +
				  	"succ='"+iPayData.getSucc()+"',resultcount='"+iPayData.getResultcount()+
				  	"',feevalue='"+String.valueOf((int)iPayData.getFeevalue())+"'" +
				  	",requestfee='"+String.valueOf((int)iPayData.getRequestfee())+
				  	"',successfee='"+String.valueOf((int)iPayData.getSuccessfee())+"' where datakey='"+iPayData.getDataKey()+"'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public IPayOrder getPayOrderByOrderId(String orderId) {
		String sql = "select id,orderid,suborderid,userid" +
				",mobileno,carrier,ordertype,codetype," +
				"spid,spcodeid,province,region,feevalue," +
				"channelid,projectid,report,ordertime" +
				",reporttime from ipayorder where orderid='"+orderId+"'";
		ModelGenerator<IPayOrder> dbcpmg = new ModelGenerator<IPayOrder>(IPayOrder.class);
		return dbcpmg.generate(dbcp, sql);
	}

}
